package org.jeecg.modules.hbjh.convert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.api.xxApi.vo.XxApiKpzdResult;
import org.jeecg.modules.common.util.Base64Util;
import org.jeecg.modules.innerApi.common.responseDto.CurrentFphmRtn;
import org.jeecg.modules.xxgx.fpht.dto.DjhtDjmxDto;
import org.jeecg.modules.xxgx.fpht.dto.DjhtDto;
import org.jeecg.modules.xxgx.fpht.dto.DjhtFpmxDto;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * 将共享返回的结果转换为金税相关返回结果
 */
@Slf4j
public class TaxResponseRtnConvert {

    /**
     * 单据推送数据转换
     */
    public static JSONObject djtsRtnConvert(Result<JSONArray> taxRtn) {
        JSONObject jsrtn = new JSONObject();
        String code = "1", msg = "";
        JSONArray jsdatas = new JSONArray();
        if (taxRtn.isSuccess()) {
            code = "0";
            msg = taxRtn.getMessage();
            JSONArray taxdatas = new JSONArray(taxRtn.getResult());
            if (taxdatas != null && taxdatas.size() > 0) {
                for (Object object : taxdatas) {
                    JSONObject taxData = JSON.parseObject(JSON.toJSONString(object));
                    JSONObject jsData = new JSONObject();
                    jsData.put("sjlx", taxData.containsKey("sjlx") ? taxData.getString("sjlx") : "");
                    jsData.put("djbh", taxData.containsKey("ywdjbh") ? taxData.getString("ywdjbh") : "");
                    jsData.put("bczt", taxData.containsKey("zt") ? taxData.getString("zt") : "");
                    jsData.put("bcsbyy", taxData.containsKey("errmsg") ? taxData.getString("errmsg") : "");
                    jsdatas.add(jsData);
                }
            }
        } else {
            code = "1";
            msg = taxRtn.getMessage();
        }
        jsrtn.put("code", code);
        jsrtn.put("msg", msg);
        jsrtn.put("data", jsdatas);
        return jsrtn;
    }

    /**
     * 数电认证到期时间查询结果数据转换
     */
    public static JSONObject sdrzdqsjcxRtnConvert(Result<JSONObject> taxRtn) {
        JSONObject jsrtn = new JSONObject();
        String code = "1", msg = "";
        JSONObject jsdata = new JSONObject();
        if (taxRtn.isSuccess()) {
            code = "0";
            msg = "成功";
            XxApiKpzdResult kpzd = (new JSONObject(taxRtn.getResult())).toJavaObject(XxApiKpzdResult.class);
            jsdata.put("kpzddm", StringUtils.isNotEmpty(kpzd.getKpzddm())?kpzd.getKpzddm():"");
            jsdata.put("kpzdmc", StringUtils.isNotEmpty(kpzd.getKpzdmc())?kpzd.getKpzdmc():"");
            jsdata.put("nsrsbh", StringUtils.isNotEmpty(kpzd.getNsrsbh())?kpzd.getNsrsbh():"");
            jsdata.put("nsrmc", StringUtils.isNotEmpty(kpzd.getNsrmc())?kpzd.getNsrmc():"");
            jsdata.put("kpr", StringUtils.isNotEmpty(kpzd.getKpr())?kpzd.getKpr():"");
            Date xcrzsj=kpzd.getXcrzsj();
            if(xcrzsj!=null) {
                String jxcrzsj=DateUtils.date2Str(xcrzsj,DateUtils.datetimeFormat.get());
                jsdata.put("jxcrzsj", jxcrzsj);
            }else{
                jsdata.put("jxcrzsj","");
            }
        } else {
            msg = taxRtn.getMessage();
        }
        jsrtn.put("code", code);
        jsrtn.put("message", msg);
        jsrtn.put("renson", msg);
        jsrtn.put("data", jsdata);
        return jsrtn;
    }

    /**
     * 纸票待开号码查询结果数据转换数据转换
     */
    public static JSONObject zpfphmcxRtnConvert(Result<JSONObject> taxRtn) {
        JSONObject jsrtn = new JSONObject();
        String code = "1", msg = "";
        JSONObject jsdata = new JSONObject();
        if (taxRtn.isSuccess()) {
            code = "0";
            msg = "成功";
            CurrentFphmRtn.CurrentFphmRtnData fphmRtnData = (new JSONObject(taxRtn.getResult())).toJavaObject(CurrentFphmRtn.CurrentFphmRtnData.class);
            jsdata.put("nsrsbh", fphmRtnData.getNsrsbh());
            jsdata.put("zppzdm", fphmRtnData.getFplxdm());
            jsdata.put("fpdm", fphmRtnData.getFpdm());
            jsdata.put("fphm", fphmRtnData.getFphm());
            jsdata.put("syfpzs", fphmRtnData.getSyfpzs());
        } else {
            msg = taxRtn.getMessage();
        }
        jsrtn.put("code", code);
        jsrtn.put("message", msg);
        jsrtn.put("renson", msg);
        jsrtn.put("data", jsdata);
        return jsrtn;
    }

    public static JSONObject fpkjRtnConvert(Result<JSONObject> taxRtn) {
        JSONObject jssuccessrtn = new JSONObject();
        JSONObject jserrorrtn = new JSONObject();
        String code = "0";
        String message = "";
        String reason = "";
        if (taxRtn.isSuccess()) {
            JSONObject result = new JSONObject(taxRtn.getResult());
            //成功
            code = "0";
            message = taxRtn.getMessage();
            String djtsjlid = result.containsKey("djtsjlid") ? result.getString("djtsjlid") : "";
            JSONObject data = new JSONObject();
            data.put("djtsjlid", djtsjlid);
            jssuccessrtn.put("czlx", "1");
            jssuccessrtn.put("code", code);
            jssuccessrtn.put("message", message);
            jssuccessrtn.put("data", data);
            return jssuccessrtn;
        } else {
            //失败
            code = "500";
            message = "失败";
            reason = taxRtn.getMessage();
            jserrorrtn.put("czlx", "1");
            jserrorrtn.put("code", code);
            jserrorrtn.put("message", message);
            jserrorrtn.put("renson", reason);
            return jserrorrtn;
        }
    }

    public static JSONObject skfpkjRtnConvert(Result<JSONObject> taxRtn) {
        String code = taxRtn.getCode().toString();
        String message = "";
        String reason = "";
        JSONObject rtn = new JSONObject();
        JSONObject jssuccessrtn = new JSONObject();
        JSONObject jserrorrtn = new JSONObject();
        if ("200".equals(code)) {
            JSONObject result = new JSONObject(taxRtn.getResult());
            rtn.put("code", "0");
            rtn.put("uuid", result.containsKey("fpsqlsh") ? result.getString("fpsqlsh") : "");
            rtn.put("msg", "开票成功");
            //成功
            message = "成功";
            jssuccessrtn.put("czlx", "1");
            jssuccessrtn.put("code", "0");
            jssuccessrtn.put("message", message);
            jssuccessrtn.put("djbh", result.containsKey("fpsqlsh") ? result.getString("fpsqlsh") : "");
            String fplxdm = result.containsKey("fplxdm") ? result.getString("fplxdm") : "";
            jssuccessrtn.put("fphm", result.containsKey("fphm") ? result.getString("fphm") : "");
            jssuccessrtn.put("fpdm", result.containsKey("fpdm") ? result.getString("fpdm") : "");
            jssuccessrtn.put("fplxdm", fplxdm);
            jssuccessrtn.put("kprq", result.containsKey("kprq") ? result.getString("kprq") : "");
            jssuccessrtn.put("jym", result.containsKey("jym") ? result.getString("jym") : "");
            jssuccessrtn.put("skm", result.containsKey("skm") ? result.getString("skm") : "");
            jssuccessrtn.put("ewm", result.containsKey("ewm") ? result.getString("ewm") : "");
            jssuccessrtn.put("jshj", result.containsKey("jshj") ? result.getString("jshj") : "");
            jssuccessrtn.put("bhsje", result.containsKey("hjje") ? result.getString("hjje") : "");
            jssuccessrtn.put("se", result.containsKey("hjse") ? result.getString("hjse") : "");

            rtn.put("content", Base64Util.encode(jssuccessrtn.toString(), "utf-8"));

            return rtn;
        } else {
            //失败
            reason = taxRtn.getMessage();
            rtn.put("code", "1");
            rtn.put("msg", reason);
            rtn.put("content", "");
            return rtn;
        }
    }

    public static JSONObject fpkjAsyncRtnConvert(JSONObject params) {
        Result result1 = JSONObject.parseObject(params.toJSONString(), Result.class);
        String code = "0";
        String message = "";
        String reason = "";
        JSONObject jssuccessrtn = new JSONObject();
        JSONObject jserrorrtn = new JSONObject();
        JSONObject result = (JSONObject) result1.getResult();
        String zt = result.containsKey("zt") ? result.getString("zt") : "";
        if ("0".equals(zt)) {
            //成功
            code = "0";
            message = "成功";
            jssuccessrtn.put("czlx", "1");
            jssuccessrtn.put("code", code);
            jssuccessrtn.put("msg", message);
            jssuccessrtn.put("djbh", result.containsKey("fpsqlsh") ? result.getString("fpsqlsh") : "");
            String fplxdm = result.containsKey("fplxdm") ? result.getString("fplxdm") : "";
            if ("01".equals(fplxdm) || "02".equals(fplxdm)) {
                jssuccessrtn.put("qdfphm", result.containsKey("fphm") ? result.getString("fphm") : "");
            } else {
                jssuccessrtn.put("fphm", result.containsKey("fphm") ? result.getString("fphm") : "");
                jssuccessrtn.put("fpdm", result.containsKey("fpdm") ? result.getString("fpdm") : "");
            }
            jssuccessrtn.put("fplxdm", fplxdm);
            jssuccessrtn.put("kprq", result.containsKey("kprq") ? result.getString("kprq") : "");
            jssuccessrtn.put("hsje", result.containsKey("jshj") ? result.getString("jshj") : "");
            jssuccessrtn.put("je", result.containsKey("hjje") ? result.getString("hjje") : "");
            jssuccessrtn.put("se", result.containsKey("hjse") ? result.getString("hjse") : "");
            return jssuccessrtn;
        } else {
            //失败
            code = "500";
            message = "失败";
            reason = result.containsKey("errmsg") ? result.getString("errmsg") : "";
            jserrorrtn.put("czlx", "1");
            jserrorrtn.put("code", code);
            jserrorrtn.put("djbh", result.containsKey("fpsqlsh") ? result.getString("fpsqlsh") : "");
            jserrorrtn.put("msg", message);
            jserrorrtn.put("renson", reason);
            return jserrorrtn;
        }
    }


    public static JSONObject fpmxRtnConvert(JSONObject params) {
        DjhtDto djhtDto = JSONObject.parseObject(params.toJSONString(), DjhtDto.class);
        JSONObject jssuccessrtn = new JSONObject();
        JSONArray mxarray = new JSONArray();
        JSONArray ywdjarray = new JSONArray();
        jssuccessrtn.put("code", "0");
        jssuccessrtn.put("message", "成功");
        jssuccessrtn.put("reason", "");
        jssuccessrtn.put("bz", "");
        String czlx = "";
        if ("0".equals(djhtDto.getKplx())) {
            czlx = "1";
        } else if ("1".equals(djhtDto.getKplx())) {
            czlx = "3";
        } else if ("3".equals(djhtDto.getKplx())) {
            czlx = "2";
        }
        jssuccessrtn.put("czlx", czlx);
        jssuccessrtn.put("djbhs", djhtDto.getYwdjbh());
        String fplxdm = djhtDto.getFplxdm();
        if ("01".equals(fplxdm) || "02".equals(fplxdm)) {
            jssuccessrtn.put("qdfphm", djhtDto.getFphm());
            jssuccessrtn.put("yqdfphm", djhtDto.getYfphm());
        } else {
            jssuccessrtn.put("fpdm", djhtDto.getFpdm());
            jssuccessrtn.put("fphm", djhtDto.getFphm());
            jssuccessrtn.put("yfpdm", djhtDto.getYfpdm());
            jssuccessrtn.put("yfphm", djhtDto.getYfphm());
        }
        jssuccessrtn.put("fplxdm", fplxdm);
        jssuccessrtn.put("je", new BigDecimal(djhtDto.getHjje()).add(new BigDecimal(djhtDto.getHjse())));
        jssuccessrtn.put("kprq", djhtDto.getKprq());
        jssuccessrtn.put("se", djhtDto.getHjse());
        jssuccessrtn.put("sjlx", djhtDto.getSjlx());
        jssuccessrtn.put("sjly", djhtDto.getSjly());
        jssuccessrtn.put("ewm", djhtDto.getEwm());
        jssuccessrtn.put("jym", djhtDto.getJym());
        jssuccessrtn.put("skm", djhtDto.getSkm());
        jssuccessrtn.put("gsdm", djhtDto.getFplxdm());
        jssuccessrtn.put("kpr", djhtDto.getKpr());
        List<DjhtFpmxDto> fpmx = djhtDto.getFpmx();
        for (int i = 0; i < fpmx.size(); i++) {
            JSONObject jsonObject = new JSONObject();
            DjhtFpmxDto djhtFpmxDto = fpmx.get(i);
            jsonObject.put("djbh", djhtFpmxDto.getYwdjbh());
            jsonObject.put("bhsdj", djhtFpmxDto.getBhsdj());
            jsonObject.put("bhsje", djhtFpmxDto.getBhsje());
            jsonObject.put("djhh", djhtFpmxDto.getMxhh());
            jsonObject.put("ggxh", djhtFpmxDto.getGgxh());
            jsonObject.put("hsdj", djhtFpmxDto.getHsdj());
            jsonObject.put("hsje", djhtFpmxDto.getHsje());
            jsonObject.put("jldw", djhtFpmxDto.getJldw());
            jsonObject.put("se", djhtFpmxDto.getTaxje());
            jsonObject.put("spdm", djhtFpmxDto.getSpdm());
            jsonObject.put("spmc", djhtFpmxDto.getSpmc());
            jsonObject.put("spsl", djhtFpmxDto.getSpsl());
            jsonObject.put("ssbm", djhtFpmxDto.getSpbm());
            jsonObject.put("tax", djhtFpmxDto.getTax());
            mxarray.add(jsonObject);
        }
        jssuccessrtn.put("mxxx", mxarray);

        List<DjhtDjmxDto> ywdjxx = djhtDto.getYwdjxx();
        for (int j = 0; j < ywdjxx.size(); j++) {
            JSONObject jsonObject = new JSONObject();
            DjhtDjmxDto djhtDjmxDto = ywdjxx.get(j);
            jsonObject.put("djbh",djhtDjmxDto.getYwdjbh());
            jsonObject.put("khmc",djhtDjmxDto.getKhmc());
            jsonObject.put("khsh",djhtDjmxDto.getKhsh());
            jsonObject.put("hjje",djhtDjmxDto.getHjje());
            jsonObject.put("hjse",djhtDjmxDto.getHjse());
            jsonObject.put("jshj",djhtDjmxDto.getJshj());
            ywdjarray.add(jsonObject);
        }
        jssuccessrtn.put("ywdjxx",ywdjarray);
        return jssuccessrtn;
    }


    public static void main(String[] args) {
    }

}
